
/******************************************************************************
 * Include files
 ******************************************************************************/
#include "../inc/main.h"
#include "init.h"
#include "global.h"
#include "motor.h"
#include "compu.h"
#include "control.h"

unsigned int hte = 0;
int32_t main(void)
{
	unsigned char DZCount = 0;

	KKN = (MAXSPEED - MINSPEED);   // 6000-200
	KKN = KKN / (NMAXVD - NMINVD); // 3525-200，kKN=1.744360902255639

	RCC_Configuration();
	LEDConfiguration();
	DR_Configuration();
	EN_Configuration();
	FG_Configuration();	   // 速度反馈脉冲
	ADC_Configuration();   // ABC相电流、输入电压、控制VSR等ADC输入
	BLDC_Configuration();  // 三相控制驱动配置
	BTIM1_Configuration(); // 定时器
	Hall_init();		   // hall配置

	for (DZCount = 0; DZCount < 200; DZCount++) // 延时等待
		;
	DIin = SampleData[0]; // A相电流初始值，ADC采样值大概为2.77V；Vo = (61−1.2×IA)/22​(单位：V)

	DZCount = 0;
	while (1)
	{
		if (TimeCountCompuSpeed > 20) // 20MS采集一次速度设定值,并检查HALL是否有误
		{
			TimeCountCompuSpeed = 0;
			SampleSpeed(); // 读取电位器并确立一个缓慢增加的速度，确定目标速度
		}

		if (Icount >= 2)
		{
			Icount = 0;
			SampleOI(); // 获取输出电流
		}

		if (TimeCountVI >= 100) // 100MS计算一次电压电流,并作一次实时速度计算
		{
			TimeCountVI = 0;
			SampleV(); // 供电电压范围10-28V，否则报警
			SampleI(); // 相电流计算

			//	RealS实际转速需*2
			RealS = hte * 100 / MPolePairs; // HALLcount1*10*60/6/MPolePairs; 100MS存储一次霍尔边沿数   //100ms*10=1s,1s*60=1min,表示一圈霍尔计数次数是6。
			if (MOTORSTATE == STATESTARTPID || MOTORSTATE == STATERUNPID)
			{
				if (RealS < 5)
				{
					DZCount++;
					if (DZCount >= 50) // 5S保护
					{
						DZCount = 0;
						ErrorCode = 7; // 堵转
					}
				}
				else
					DZCount = 0;
			}
		}

		if (ErrorCode != 0 && MOTORSTATE != STATEERROR && MOTORSTATE != STATEERROROVER) // 发生故障
		{
			MOTORSTATE = STATEERROR;
		}

		switch (MOTORSTATE)
		{
		case STATESTARTCHECK:
			MotorStartCheck();
			break;
		case STATESTARTDELAY:
			MotorStartDealy();
			ENCheck();
			break;
		case STATESTARTPID:
			MotorStartPID();
			ENCheck();
			break;
		case STATERUNPID:
			MotorRunPID();
			ENCheck();
			break;
		case STATESTOP:
			MotorStop();
			break;
		case STATEERROR:
			MotorError();
			break;
		case STATEERROROVER:
			MotorErrorOver();
			break;
		case STATWAITSTART:
			WaitStart();
			break;
		}
	}
}

void BTIM1_IRQHandler(void) // 1ms
{
	/* USER CODE BEGIN */
	if (BTIM_GetITStatus(CW_BTIM1, BTIM_IT_UPDATE))
	{
		BTIM_ClearITPendingBit(CW_BTIM1, BTIM_IT_UPDATE);

		Icount++;
		TimeCountTemp++;	   // 临时计时用
		TimeCountCompuSpeed++; // 计算设定速度
		TimeCountSTDelay++;	   // 启动延时

		TimeCountRealSpd++; // 瞬时速度计明用
		if (TimeCountRealSpd >= 20)
		{
			HALLcountTemp = HALLcount;
			HALLcount = 0;
			TimeCountRealSpd = 0;
		}

		TimeCountPID++;	   // PID定时计数用
		TimeCountAvgSpd++; // 平均速度计数用
		if (TimeCountAvgSpd >= 100)
		{
			hte = HALLcount1;
			HALLcount1 = 0;
			TimeCountAvgSpd = 0;
		} // 用于显示的速度值或堵转保护的速度
		TimeCountStartUP++; // 电机启动过程时，加速计数计时用
		TimeCountVI++;		// 计算电压电流
	}
	/* USER CODE END */
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
	/* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
	   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
	/* USER CODE END 6 */
}
#endif
